package com.hy;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:飞地的数量
 * 给你一个大小为 m x n 的二进制矩阵 grid ，其中 0 表示一个海洋单元格、1 表示一个陆地单元格。
 *
 * 一次 移动 是指从一个陆地单元格走到另一个相邻（上、下、左、右）的陆地单元格或跨过 grid 的边界。
 *
 * 返回网格中 无法 在任意次数的移动中离开网格边界的陆地单元格的数量。
 *
 * 做法：提供两种做法DFS和BFS
 * author: Mr.Du
 * Date: 2023/9/2
 * Time: 10:12
 */
public class NumEnclaves {

    int count = 0;
    boolean[][] visited;
    int[][] dir = {
            {0, 1}, {1, 0}, {0, -1}, {-1, 0}
    };

    /**
     * 解决思路和Solve一样，都是从四个边开始出发，将与边界相连的1都置为0，之后，再重新计算1的数量即可
     * @param grid
     * @return
     */
    public int numEnclavesWithDfs(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        for(int i = 0;i < m;i++){
            if(grid[i][0] == 1) dfs(grid, i, 0, m, n);
            if(grid[i][n - 1] == 1) dfs(grid, i, n - 1, m, n);
        }
        for(int i = 0;i < n;i++){
            if(grid[0][i] == 1) dfs(grid, 0, i, m, n);
            if(grid[m - 1][i] == 1) dfs(grid, m - 1, i, m, n);
        }
        count = 0;
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                if(grid[i][j] == 1){
                    dfs(grid, i, j, m, n);
                }
            }
        }
        return count;
    }

    public void dfs(int[][] grid, int i, int j, int m, int n){

        if(i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) return;
        grid[i][j] = 0;
        count++;
        dfs(grid, i, j + 1, m, n);
        dfs(grid, i, j - 1, m, n);
        dfs(grid, i - 1, j, m, n);
        dfs(grid, i + 1, j, m, n);
    }


    public void bfs(int[][] grid, int x, int y, int m, int n){
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(x);
        queue.offer(y);
        grid[x][y] = 0;
        count++;
        while(!queue.isEmpty()){
            int xx = queue.poll();
            int yy = queue.poll();
            for(int i = 0;i < 4;i++){
                int cxx = xx + dir[i][0];
                int cyy = yy + dir[i][1];
                if(cxx < 0 || cxx >= m || cyy < 0 || cyy >= n || grid[cxx][cyy] == 0) continue;
                queue.offer(cxx);
                queue.offer(cyy);
                count++;
                grid[cxx][cyy] = 0;
            }
        }
    }
    public int numEnclavesWithBfs(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        for(int i = 0;i < m;i++){
            if(grid[i][0] == 1) bfs(grid, i, 0, m, n);
            if(grid[i][n - 1] == 1) bfs(grid, i, n - 1, m, n);
        }
        for(int i = 0;i < n;i++){
            if(grid[0][i] == 1) bfs(grid, 0, i, m, n);
            if(grid[m - 1][i] == 1) bfs(grid, m - 1, i, m, n);
        }
        count = 0;
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                if(grid[i][j] == 1){
                    bfs(grid, i, j, m, n);
                }
            }
        }
        return count;
    }

    public void bfsWithVisited(int[][] grid, int x, int y, int m, int n){
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(x);
        queue.offer(y);
        grid[x][y] = 0;
        visited[x][y] = true;
        while(!queue.isEmpty()){
            int xx = queue.poll();
            int yy = queue.poll();
            for(int i = 0;i < 4;i++){
                int cxx = xx + dir[i][0];
                int cyy = yy + dir[i][1];
                if(cxx < 0 || cxx >= m || cyy < 0 || cyy >= n
                        || grid[cxx][cyy] == 0 || visited[cxx][cyy]) continue;
                queue.offer(cxx);
                queue.offer(cyy);
                grid[cxx][cyy] = 0;
                visited[cxx][cyy] = true;
            }
        }
    }


    /**
     * bfs使用标记位  16ms -> 7ms
     * @param grid
     * @return
     */
    public int numEnclavesBfsWithVisited(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        visited = new boolean[m][n];
        for(int i = 0;i < m;i++){
            if(grid[i][0] == 1) bfsWithVisited(grid, i, 0, m, n);
            if(grid[i][n - 1] == 1) bfsWithVisited(grid, i, n - 1, m, n);

        }
        for(int i = 0;i < n;i++){
            if(grid[0][i] == 1) bfsWithVisited(grid, 0, i, m, n);
            if(grid[m - 1][i] == 1) bfsWithVisited(grid, m - 1, i, m, n);
        }
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                if(grid[i][j] == 1 && !visited[i][j]){
                    count++;
                }
            }
        }
        return count;
    }

    /**
     * dfs使用标记位9ms -> 7ms
     * @param grid
     * @return
     */
    public int numEnclavesDfsWithVisited(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        visited = new boolean[m][n];
        for(int i = 0;i < m;i++){
            if(grid[i][0] == 1) dfsWithVisited(grid, i, 0, m, n);
            if(grid[i][n - 1] == 1) dfsWithVisited(grid, i, n - 1, m, n);

        }
        for(int i = 0;i < n;i++){
            if(grid[0][i] == 1) dfsWithVisited(grid, 0, i, m, n);
            if(grid[m - 1][i] == 1) dfsWithVisited(grid, m - 1, i, m, n);
        }
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                if(grid[i][j] == 1 && !visited[i][j]){
                    count++;
                }
            }
        }
        return count;
    }

    public void dfsWithVisited(int[][] grid, int i, int j, int m, int n){

        if(i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) return;
        grid[i][j] = 0;
        visited[i][j] = true;
        dfsWithVisited(grid, i, j + 1, m, n);
        dfsWithVisited(grid, i, j - 1, m, n);
        dfsWithVisited(grid, i - 1, j, m, n);
        dfsWithVisited(grid, i + 1, j, m, n);
    }

}
